﻿
local Log = require("common.Log")
local Skill = require("skill.Skill")
local SkillDefine = require("skill.Define")
local SkillLearn = require("skill.Learn")
local Conditions	= require("skill.Conditions")
local SkillExcel = require("excel.Skill").Skill
local UtilityExcel	= require("excel.Skill").Utility
local Obj = require("core.Obj")
local ObjHuman = require("core.ObjHuman")
local Msg = require("core.Msg")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local PetLogic = require("pet.PetLogic")
local CopyL = require("copy.CopyL")
local BeginStory = require("scene.BeginStory")
local Status = require("scene.Status")
local Timer = require("core.Timer")

local ACTTACK_TYPE_LOCK_TARGET  			= SkillDefine.ACTTACK_TYPE_LOCK_TARGET  
local ERR_SKILL_DIE							= SkillDefine.ERR_SKILL_DIE
local ERR_SKILL_FRIEND						= SkillDefine.ERR_SKILL_FRIEND

function CG_SKILL_FIRE(human,m)
	local target = Obj.objs[m.recv_id]
	local mm	 = Msg.gc.GC_SKILL_ERR
	if human.db.hp < 1 then
		mm.ret = ERR_SKILL_DIE
		Msg.send(mm,human.fd)
		return
	end
--	print("m.skill_id:", m.skill_id)
	local skillID = m.skill_id
	-- 判断当前是否能使用回城技能
	if skillID == SkillDefine.SKILL_BACK_HOME_ID then
		local copyID = CopyL.isCopyBySceneID(human.scene_id)
		if (copyID and BeginStory.isNoExitCopy(copyID)) or human.db.lv < 30 then
			mm.ret = SkillDefine.ERR_SKILL_NO_BACK_HOME
			Msg.send(mm,human.fd)
			return
		end
	end
	local skillConfig = SkillExcel[skillID]
	if not skillConfig then
	   return
	end
	if human.db.isMount ~= 1 then
	   mm.ret= SkillDefine.ERR_SKILL_NO_HORSE
	   Msg.send(mm, human.fd)
	   return
	end
	-- 判断敌友关系
	if target and human ~= target then
		local mainUtility				= skillConfig.mainUtility	
		local mustUtility				= skillConfig.mustUtility	
		local mainUtilityConfig 		= UtilityExcel[mainUtility]
		local mustUtilityConfig 		= UtilityExcel[mustUtility]
		
		if mainUtilityConfig.acttackType==ACTTACK_TYPE_LOCK_TARGET and Obj.getHp(target)>0
			and not Conditions.friendAndFoeConditions(human, target, mainUtilityConfig.friendAndFoeConditions)
			and (not mustUtilityConfig or not Conditions.friendAndFoeConditions(human, target, mustUtilityConfig.friendAndFoeConditions)) then
			
			mm.ret = ERR_SKILL_FRIEND	-- 不能对友方使用技能
			Msg.send(mm, human.fd)
			return
		end
	end
	
	-- 获取技能等级
	local skillLevel = SkillLearn.getLevel(human, skillID) + ObjHuman.getSkillLevelUp(human, skillConfig.group)
	
	if human.gm_skill_id == skillID then
	   skillLevel = human.gm_skill_lv
	end
	local ret = Skill.use(human,skillID,m.tx,m.ty,m.x, m.y, target, skillLevel, true)

	if ret==true then 
--		EquipCallSkill.onHumanUseSkill(human, target, SkillDefine.EQUIP_CALL_SKILL_CONDITION_USE_SKILL, m.tx, m.ty, skillID)
	elseif ret~=SkillDefine.ERR_SKILL_USE and ret ~= ERR_SKILL_FRIEND then
		mm.ret = ret
		Msg.send(mm,human.fd)
	end
end

function CG_SKILL_STOP(human)
	Buffer.onSkillStop(human)
end

function CG_SKILL_UPGRADE(human,m)
  if m.from < 0 or m.from > 3 then
      return
  end 
	local skillConfig = SkillExcel[m.id]
	if not skillConfig then return end
	local flag,err = SkillLearn.upgrade(human,m.id, m.from)
	if err then
		err = "升级 "..skillConfig.name.." 失败，"..err
	else
		err = "升级"..skillConfig.name.."成功"
	end
	
	if flag then
		Log.write(Log.LOGID_OSS_SKILL_UPGRADE, Log.makeHumanInfo(human),m.id, SkillLearn.getLevel(human,m.id))
	end
	Broadcast.sendBroadcast(human, Lang.MOUSE, err)

	if flag and m.from == 0 then
--		SkillNotify.GC_SKILL_LIST(human)		
--		ShenbingLogic.updateShenbing(human, skillConfig.sb)
  elseif flag and m.from == 1 then
    PetLogic.updatePet(human, m.petId)
	end
end

function CG_SKILL_CLIENT_HIDE(human, m)
    human.db.hide = m.hide > 0 and 1 or 0
end


function CG_SKILL_MOUSE_POINT(human, msg)
   if msg.mousex < 0 or msg.mousey < 0 then
      return
   end
   if human.status ~= Status.HUMAN_STATUS_GUIDE then
      return
   end
   if human.skill_mousex == msg.mousex and human.skill_mousey == msg.mousey then
      return
   end
   human.skill_mousex = msg.mousex
   human.skill_mousey = msg.mousey
   human.skill_mouse_time = Timer.now
   local mm = Msg.gc.GC_SKILL_MOUSE_POINT
   mm.mousex = msg.mousex
   mm.mousey = msg.mousey
   mm.objid = human.id
   Obj.sendArea(mm,  human.scene_id, human.x, human.y, human.fd)
end


